package app.musicPlayer.domain;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import app.persistencia.CancionLista;
import app.persistencia.ListaDeReproduccion;
import app.persistencia.ListasDeReproduccion;
import app.utils.Utiles;

public class ViewSongByList extends ListActivity {
	
	CancionLista cancionSelecionada;
	ArrayAdapter<CancionLista> adapter;
	ArrayList<CancionLista> arrayCanciones;
	Integer idListSelected;
	
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
		Bundle bundle = getIntent().getExtras();
        String[] values = new String[1];
        idListSelected = Integer.valueOf(bundle.getString("ID"));
        
		ListasDeReproduccion listas = ListasDeReproduccion.getIntances();
		arrayCanciones = listas.getListaDeReproduccion(idListSelected).getListaRep();
		// Create an array of Strings, that will be put to our ListActivity
		adapter = new InteractiveArrayAdapterListaReproduccion(this,arrayCanciones);
		setListAdapter(adapter);
		final ListView listsview = getListView();
		listsview.setLongClickable(true);
        listsview.setOnItemLongClickListener(new OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> parent, View v, int posicion, long id) {
            	cancionSelecionada = arrayCanciones.get(posicion);
       			registerForContextMenu(listsview);
				return false;
        }
    });
		listsview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int posicion, long id) {
            	cancionSelecionada = arrayCanciones.get(posicion);
    			goReproducir();
            }
        });
	}

	protected void goReproducirLista() {

		ReproduccionActual.getIntances().assignNewSongsList(arrayCanciones);
	 	Intent intent = new Intent(ViewSongByList.this, Mp3player.class);
    	finish();
    	startActivity(intent);
		
	}

	@Override  
	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
    	super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Menu Lista");  
		menu.add(0, v.getId(), 0, "Editar");  
		menu.add(0, v.getId(), 0, "Reproducir");
		menu.add(0, v.getId(),0, "Borrar de la lista");
	}
	 @Override  
		public boolean onContextItemSelected(MenuItem item) {  
	    	if(item.getTitle()=="Editar"){
	    		goEditar();
	    	}  
	    	else if(item.getTitle()=="Borrar de la lista"){
	    		borrar();
	    	}
	    	else if(item.getTitle()=="Reproducir"){
	    		goReproducir();
	    	}

	    	else {return false;}  
	    	return true;  
	    }

	private void goReproducir() {
		ArrayList<CancionLista>una = new ArrayList<CancionLista>();
		una.add(cancionSelecionada);
		ReproduccionActual.getIntances().assignNewSongsList(una);
	 	Intent intent = new Intent(ViewSongByList.this, Mp3player.class);
    	finish();
    	startActivity(intent);
	}

	private void borrar() {
		AlertDialog alertDialog = new AlertDialog.Builder(ViewSongByList.this).create();
    	alertDialog.setTitle("Borrar");
		alertDialog.setMessage("¿Estas seguro que deseas borrar la cancion de la lista de reproduccion?");
 	    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
    	      public void onClick(DialogInterface dialog, int which) {
    	 
	    	    ListasDeReproduccion.getIntances().removeSong(ViewSongByList.this, idListSelected, 
    	  				cancionSelecionada.getTittle(), cancionSelecionada.getPosition());
    	  		Bundle b = new Bundle();
    	  		b.putString("ID", String.valueOf(idListSelected));
    	  		Utiles.showMessage(ViewSongByList.this, "Se ha borrado con exito");

    	  		returnToActualView(b);
    	    } });
     	  alertDialog.setButton2("CANCEL", new DialogInterface.OnClickListener() {
   	      public void onClick(DialogInterface dialog, int which) {
   	    	  
   	    } });
     	alertDialog.show();	
		
	}
	private void returnToActualView(Bundle b) {
    	Intent intent = new Intent(ViewSongByList.this, ViewSongByList.class);
    	if(b != null) {
    		intent.putExtras(b);
    	}
    	finish();
    	startActivity(intent);
	}
	private void goEditar() {
  	  Utiles.showMessage(ViewSongByList.this, "Proximamente...");
	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	        //moveTaskToBack(true);          
	    	returnToViewMain(null);
	    	return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
	private void returnToViewMain(Object object) {
    	Intent intent = new Intent(ViewSongByList.this, SocialMusicPlayerActivity.class);
    	finish();
    	startActivity(intent);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.viewsongsmenu, menu);
	    return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.seleccion:
	        	System.out.println("Hola");
	            seleccion();
	            return true;
	        case R.id.Volver :
	        	returnToViewMain(null);
	        	return true;
	        case R.id.reproducirLista:
	        	goReproducirLista();
	        	return true;
	       default :
	            return super.onOptionsItemSelected(item);
	    }
	}

	private void seleccion() {
		Intent intent = new Intent(ViewSongByList.this,SongsByListSelection.class);
		Bundle b = new Bundle();
		b.putString("ID", String.valueOf(idListSelected));
		intent.putExtras(b);
		finish();
		startActivity(intent);	
	}
}
